<?php

namespace Common\Model;

/**
 * AdminHotelModel.class实例模型
 * @author songanwei
 * @date 2015-12-31
 * @index adminhotel(`isdelete`, `adminlogin_id`, `hotelentity_id`, `role`)
 */
Class AdminHotelModel extends \Common\Model\BaseModel {

    protected $trueTableName = 'tb_admin_hotel';

    /**
     * 返回客栈管理员客栈关系数据
     * @modify songanwei 2016-01-04
     * @modify trainyao 2016-03-23 增加查询多个
     * @modify wenhui 2017-03-27 增加hotel
     * @param integer $adminId  管理员ID
     * @param integer $role 角色ID
     * @return type
     */
    public function getInfosByAdmin($adminId, $role = 1, $hotelId = null) {
        if (is_array($adminId)) {
            $adminId = implode(',', $adminId);
        }
        $where = array(
            'role' => $role,
            'isdelete' => NOT_DELETE,
        );
        if (strpos($adminId, ',') === false) {
            $where['adminlogin_id'] = $adminId;
        } else {
            $where['adminlogin_id'] = array('in', $adminId);
        }

        if (!is_null($hotelId)) {
            $where['hotelentity_id'] = $hotelId;
        }

        return $this->where($where)->select();
    }

    /**
     * 
     * @param type $hotelId
     * @param type $role
     * @return type
     */
    public function getInfosByHotel($hotelId, $role = 2) {
        if (is_array($hotelId)) {
            $hotelId = implode(',', $hotelId);
        }
        if (strpos($hotelId, ',') !== false) {
            $where['hotelentity_id'] = array('in', $hotelId);
        } else {
            $where['hotelentity_id'] = $hotelId;
        }

        $where['isdelete'] = NOT_DELETE;
        $where['role'] = $role;
        return $this->where($where)->select();
    }

    /**
     * 返回客栈管理员数据
     * @modify songanwei 2016-01-05
     * @param integer $adminId  管理员ID
     * @param type $hotelId 客栈ID
     * @return type
     */
    public function getInfoByAdminHotel($adminId, $hotelId) {
        $where = array(
            'isdelete' => NOT_DELETE,
            'hotelentity_id' => $hotelId,
            'adminlogin_id' => $adminId
        );

        if (is_array($hotelId)) {
            $where['hotelentity_id'] = array('in', $hotelId);
            return $this->where($where)->select();
        }

        return $this->where($where)->find();
    }

    /**
     * 返回客栈管理员数据
     * @modify zhangzhen 2017-02-17
     * @param integer $adminId  管理员ID
     * @param type $hotelId 客栈ID
     * @return type
     */
    public function getHotelCreator($hotelId) {
        $where = array(
            'hotelentity_id' => $hotelId,
            'role' => 1
        );
        return $this->where($where)->find();
    }

    /**
     * 返回客栈管理员数据
     * @modify zhangzhen 2017-02-17
     * @param integer $adminId  管理员ID
     * @param type $hotelId 客栈ID
     * @return type
     */
    public function getUserInfo($hotelId) {
        $where = array(
            'hotelentity_id' => $hotelId,
            'isdelete' => 0
        );
        return $this->where($where)->select();
    }

    /**
     * 
     * 添加客栈管理员关系
     * @modify songanwei 2015-12-31
     * @param integer $hotelId 客栈ID
     * @param integer $adminId 管理员ID
     * @param integer $role  1=创建者 2=普通管理员
     * @return type
     */
    public function addInfo($hotelId, $adminId, $role = 2) {
        $options = array(
            'hotelentity_id' => $hotelId,
            'adminlogin_id' => $adminId,
            'role' => $role
        );
        return $this->add($options);
    }

    /**
     *
     * 获取管理员-客栈一对多关系
     * @modify trainyao 2016-01-04 创建
     * @param  integer   $userId    管理员id
     * @return array          关系数据
     */
    public function getInfoByUser($userId) {
        $option = array(
            'isdelete' => NOT_DELETE,
            'adminlogin_id' => $userId,
        );
        $fields = 'isdelete,adminlogin_id,hotelentity_id,role';
        $data = $this->where($option)->select();

        if (empty($data)) {
            return array();
        }
        return $data;
    }

    /**
     *
     * 获取管理员-客栈一对多关系 并获取开启状态
     * @modify wenhui 2017-08-11 创建
     * @param  integer   $userId    管理员id
     * @return array          关系数据
     */
    public function getInfoByUserLeftHotelEntity($userId) {
        $option = array(
            'isdelete' => NOT_DELETE,
            'adminlogin_id' => $userId,
            'tb_hotel_entity.status' => 0,
            'tb_hotel_entity.enddate' => array('gt', date('Y-m-d')),
            'tb_hotel_entity.usage' => array('neq', 0),
        );

        $fields = 'tb_hotel_entity.status as status,isdelete,adminlogin_id,hotelentity_id,role';
        $data = $this->field($fields)
                     ->join('tb_hotel_entity ON tb_hotel_entity.id = tb_admin_hotel.hotelentity_id','LEFT')
                     ->where($option)
                     ->select();

        if (empty($data)) {
            return array();
        }
        return $data;
    }

    /**
     * 删除管理员客栈数据
     * @modify songanwei 2015-12-31
     * @param integer $adminId  管理员ID
     * @param integer $hotelId 客栈ID
     * @return type
     */
    public function deleteInfoAdminHotel($adminId, $hotelId) {
        $where = array(
            'isdelete' => NOT_DELETE,
            'adminlogin_id' => $adminId,
        );
        if (is_array($hotelId)) {
            $where['hotelentity_id'] = array('in', $hotelId);
        } else {
            $where['hotelentity_id'] = $hotelId;
        }
        
        $result = $this->where($where)->save(array('isdelete' => IS_DELETE));
        return $result === false ? false : true;
    }

    /**
     * 检查用户是否存在客栈内
     * @modify songanwei 2016-01-04
     * @param integer $adminId  客栈管理员ID
     * @param mixed $hotelIdArr  客栈ID数组或客栈ID
     * @return type
     */
    public function checkExistAdmin($adminId, $hotelIdArr = array()) {
        $where = array(
            'adminlogin_id' => $adminId,
            'isdelete' => NOT_DELETE
        );
        if (is_array($hotelIdArr)) {
            $where['hotelentity_id'] = array('in', $hotelIdArr);
        } else {
            $where['hotelentity_id'] = $hotelIdArr;
        }

        $result = $this->where($where)->find();
        return $result ? true : false;
    }

    /**
     * 返回客栈管理员数据
     * @modify wenhui 2017-12-18
     * @param type $hotelId 客栈ID
     * @return type
     */
    public function getUserInfoByHotel($hotelId) {
        $where = array(
            'hotelentity_id' => $hotelId,
        );
        return $this->where($where)->select();
    }

}
